package com.enfo.zjabank.service;

import com.enfo.zjabank.dao.UploadDao;
import com.enfo.zjabank.model.UploadedFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhoum on 2016/7/22.
 */
@Service
public class UploadServiceImpl implements UploadService {
    private static Logger logger = LoggerFactory.getLogger(UploadServiceImpl.class);
    @Autowired
    private UploadDao uploadDao;

    /**
     * 上传文件信息入库
     * @param ufile
     */
    public int upload(UploadedFile ufile,String busiCode){
        if(busiCode.startsWith("change")){
            return uploadDao.uploadModify(ufile);
        }else{
            return uploadDao.upload(ufile);
        }
    }

    @Override
    public Map<String,Object> getImg(String busiCode,String id) {
        return uploadDao.getFile(busiCode,id);
    }

    @Override
    public List<Map<String, Object>> listImgs(String busiCode, String busiId) {
        return uploadDao.listImgs(busiCode,busiId);
    }

    @Override
    public Map<String, List> del(String busiCode,String id) {
        Map<String,Object> file = uploadDao.getFile(busiCode,id);
        int i = uploadDao.del(busiCode,id);
        if((!busiCode.startsWith("change") && i>0) || (busiCode.startsWith("change") && i>0 && file.get("attachmentid")==null)){
            new File((String) file.get("path")).delete();
            if("image".equals(file.get("file_type"))){
                new File((String) file.get("thumb_path")).delete();
            }
        }
        Map<Object, Boolean> map = new HashMap<>();
        map.put(file.get("origin_name"), true);
        List<Map> list = new ArrayList<>();
        list.add(map);
        Map<String,List> returnValue = new HashMap<>();
        returnValue.put("files",list);
        return returnValue;
    }

    @Override
    public boolean uploadRole(String inputCode) {
        return uploadDao.uploadRole(inputCode);
    }

    @Override
    public Map getDebtInfo(String debtId) {
        return uploadDao.getDebtInfo(debtId);
    }

    @Override
    public Map getDebtChangeInfo(String debtId) {
        return uploadDao.getDebtChangeInfo(debtId);
    }

    public String getInputCode(String debtId){
        return uploadDao.getInputCode(debtId);
    }

    @Override
    public String getDebtId(String modifyId) {
        return uploadDao.getDebtId(modifyId);
    }

    /**
     * 附件修改审核通过(在快照表记录历史数据,再将修改表的变更覆盖到附件表)
     * @param modifyId
     */
    @Override
    @Transactional
    public void attachementsChange(String modifyId) {
        uploadDao.createSnapshot(modifyId);
        uploadDao.mergeModify(modifyId);
    }

    /**
     * 根据状态显示附件(已撤销查询)
     * @param debtId
     * @return
     */
    @Override
    public String showByState(String debtId) {
        Map<String,Object> debt = uploadDao.getDebtInfo(debtId);
        String report_state = (String) debt.get("report_state");
        if("1070".equals(report_state)){
            return "noticecancel";
        }else if("1080".equals(report_state)){
            return "debt";
        }else{
            return null;
        }
    }
}
